//
//  AppDelegate.swift
//  TTProduct
//
//  Created by Toj on 1/4/21.
//
import Cocoa
import WebKit

// find /Users/vipcode/Library/Developer/Xcode/DerivedData/TT-gjyvybfzbhwdcuawfiutseynndal/Build/Products/Release/TTProduct-macOS.app -name "*.framework" | sed -E 's_.*/([^/]+)_\1_' | sort

@_exported import TTBLL_macOS
@_exported import TTCOM_macOS
@_exported import TTCoreData_macOS

public let TTScreenVisibleWidth = NSScreen.main?.visibleWidth ?? 0
public let TTScreenVisibleHeight = NSScreen.main?.visibleHeight ?? 0

@main
class AppDelegate: NSObject {
    
    private var keyWindow: NSWindow?
    private var windowController: TTWindowController?
}

extension AppDelegate: NSApplicationDelegate {
    
    // 默认通知中心在应用程序对象初始化之前立即发送。
    func applicationWillFinishLaunching(_ notification: Notification) {
        setupMakePrepareExecute()
    }
    
    // 默认的通知中心在应用程序启动和初始化之后但收到第一个事件之前发送。
    func applicationDidFinishLaunching(_ notification: Notification) {
         setupMakeRootWindowController()
         windowController?.showWindow(self)
    }
    
    // 终止前
    func applicationWillTerminate(_ notification: Notification) {
        print(className + " : " + #function)
    }
    
    // 重新打开
    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
        print(flag)
        if !flag {
            if windowController == nil {
                setupMakeRootWindowController()
            }
            windowController?.showWindow(self)
        }
        return true
    }
}

extension AppDelegate {
    
    private func setupMakePrepareExecute() {
   
        
        let fileURL = "/Users/vipcode/Desktop/TESTFile/R"
        let filePath = "/Users/vipcode/Desktop/TESTFile/txt.txt"
        
       let isExists = FileManager.default.fileExists(atPath: fileURL)
        print("isExists:\(isExists)")
        
//        let url = URL(fileURLWithPath: fileURL)
//        try? FileManager.default.removeItem(at: url)
//
        return
        
        let loginLogic = LoginLogic()
        let title = loginLogic.loginTitle()
        print(title)
        
        print("TT".tt_localized)
        
        let quality = min(2, 4)
        print(quality)
        
        let host = "taojian.com"
        let api = "api"
        let vipcode = "/vipcode"

        print(host.tt_appending(path: api))
        print(host.tt_appending(path: vipcode))
    }
    
    private func setupMakeRootWindowController() {
        keyWindow = setupMakeWindow()
        windowController = TTWindowController(window: keyWindow)
        keyWindow?.delegate = self
    }
    
    private func setupMakeWindow() -> NSWindow {
         let vc = ViewController()
        // let vc = CollectionViewController()
        // let vc = WhiteboardViewController()
        // let vc = TTHomeViewController()
//        let vc = OSSViewController()
        
        
        let window = TTWindow(contentViewController: vc)
        let rect = CGRect(x: 0, y: 0, width: 1000, height: 960)
        window.tt_titleBarHeight = 50
        window.tt_isMove = false
        window.setContentSize(rect.size)
        window.center()
        return window
    }
}

extension AppDelegate: NSWindowDelegate {
    func windowWillClose(_ notification: Notification) {
        let windows = NSApp.windows
        if windows.count == 1 {
            windowController = nil
            keyWindow = nil
        }
    }
    
    func windowWillEnterFullScreen(_ notification: Notification) {
        // print(className + " : " + #function)
        guard let window = windowController?.window as? TTWindow else { return }
        window.tt_isFullScreen = true
    }
    
    func windowDidEnterFullScreen(_ notification: Notification) {
        // print(className + " : " + #function)
        guard let window = windowController?.window as? TTWindow else { return }
        window.tt_windowDidFullScreen()
    }
    
    func windowWillExitFullScreen(_ notification: Notification) {
        // print(className + " : " + #function)
        guard let window = windowController?.window as? TTWindow else { return }
        window.tt_isFullScreen = false
    }
    
    func windowDidExitFullScreen(_ notification: Notification) {
        // print(className + " : " + #function)
        guard let window = windowController?.window as? TTWindow else { return }
        window.tt_windowDidFullScreen()
    }
}
